package com.atguigu.spzx.manner.service.impl;

import com.atguigu.spzx.manner.mapper.SystemUserMapper;
import com.atguigu.spzx.manner.service.SystemUserService;
import com.atguigu.spzx.model.dto.LoginDto;
import com.atguigu.spzx.model.dto.SysUserDto;
import com.atguigu.spzx.model.entity.SysRole;
import com.atguigu.spzx.model.entity.SysUser;
import com.atguigu.spzx.model.vo.LoginVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.alibaba.fastjson.JSON;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestHeader;
import spzx.util.AtguiguAssert;

import java.util.List;
import java.util.UUID;
@Service
public class SystemUserServiceImpl implements SystemUserService {
    @Autowired
    SystemUserMapper systemUserMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Override
    public LoginVo login(LoginDto loginDto) {
        System.out.println("123");
        System.out.println("123");
        System.out.println("123");



        String userName = loginDto.getUserName();
        String captcha = loginDto.getCaptcha();
        String codeKey = loginDto.getCodeKey();
        String cahcaptcha =(String) redisTemplate.opsForValue().get("captcha:" +codeKey);
        AtguiguAssert.isTrue(StringUtils.hasText(captcha)&&captcha.equals(cahcaptcha), "验证码错误");

        SysUser sysUser = systemUserMapper.selectByUserName(userName);
//        if (sysUser==null) {
//            return null;
//
//        }
        AtguiguAssert.notNull(sysUser, "用户名不存在");
        String inputPassword = loginDto.getPassword();
        String md5InputPassword = DigestUtils.md5DigestAsHex(inputPassword.getBytes());
//        if (!md5InputPassword.equals(password)) {
//
//        }
        AtguiguAssert.isTrue(md5InputPassword.equals(sysUser.getPassword()), "用户名或者密码错误");
        String token = UUID.randomUUID().toString().replace("-", "");
        AtguiguAssert.notNull(token,"token无效");
        if (md5InputPassword.equals(sysUser.getPassword())) {
            LoginVo loginVo = new LoginVo();
            loginVo.setToken(token);
//            loginVo.setRefresh_token("");
            redisTemplate.opsForValue().set("user:login:"+token,JSON.toJSONString(sysUser));
            return loginVo;
        }
        return null;
    }

    @Override
    public SysUser getUserInfo(@RequestHeader("token") String token) {
        //拿着token找用户
        String json= (String) redisTemplate.opsForValue().get("user:login:"+token);
//        if (StringUtils.isEmpty(json)) {
//
//        }
        AtguiguAssert.isTrue(!StringUtils.isEmpty(json), "用户不存在");
        SysUser sysUser = JSON.parseObject(json, SysUser.class);
//        SysUser sysUser = new SysUser();
//        sysUser.setUserName("立杆");

        return sysUser;
    }

    @Override
    public PageInfo<SysUser> findByPage(Integer pageNum, Integer pageSize, SysUserDto sysUserDto) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> roleList = systemUserMapper.selectfindByPage(sysUserDto);

        return new PageInfo<>(roleList);
    }

    @Override
    public void updateSysRole(SysUser sysUser) {
        systemUserMapper.updateByPrimaryKeySelective(sysUser);
    }

    @Override
    public void saveSysRole(SysUser sysUser) {
        String password = sysUser.getPassword();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        sysUser.setPassword(md5Password);
        sysUser.setStatus(1);
        systemUserMapper.insertSelective(sysUser);
    }

    @Override
    public void deleteSysUserById(Long id) {
        systemUserMapper.deleteByPrimaryKey(id);
    }

}
